<!Doctype html>
<html>

<head>
    <title>oixan的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html" ; charset="utf-8">
    <link rel="stylesheet" type="text/css" href="../css/blog.css">
</head>

<body>
    <div class="head">
        <div class="headnav">
            <div class="naveframe">
                <ul class="navlist">
                    <li>
                        <a href="../index.html">
                            主页
                        </a>
                    </li>
                    <li>
                        <a href="../bloglist.html">
                            博客
                        </a>
                    </li>
                    <li>
                        <a href="../about.html">
                            关于
                        </a>
                    </li>
                </ul>
            </div>
        </div>

        <div class="headtext">
            <div class="headtext1">世界很简单，人类很复杂。</div>
            <div class="headtext2">浮生若梦，寻寻觅觅。</div>
        </div>
    </div>

    <div class="contents-all">
        <div class="contents">

            <div class="frame">
                <div class="title">
                    <h3>初看python的socket</h3>
                </div>
                <div class="dtime">时间：2018-06-13</div>
                <div class="detail">

                        <h3>
                                <span style="font-family: 宋体, SimSun;">服务器端</span>
                            </h3>
                            <p>
                                <span style="font-size: 14px;">import socket</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">import threading</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">import os</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">import select</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##服务端</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">def tcpserve():</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global clients</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global inputs</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global outputs</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; if not os.path.exists(&#39;d:/chat/&#39;):</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; &nbsp; &nbsp; os.makedirs(&#39;d:/chat/&#39;)</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; while True:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; stdinput, stdoutput, stderr = select.select(inputs, outputs, inputs)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; for obj in stdinput:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if obj is s:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c,addr=s.accept()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputs.append(c)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients[addr]=c</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for l in clients:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients[l].sendall(&#39;{}已登入&#39;.format(str(addr)).encode(&#39;utf-8&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;{}已登入&#39;.format(str(addr)))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pass</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acceptmsg=str(obj.recv(1024),encoding=&#39;utf-8&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;&#39;.join([acceptmsg,&#39; 客户端口：&#39;,str(addr[1])]))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accptdata=&#39;&#39;.join([acceptmsg,&#39; 客户端口：&#39;,str(addr[1])])</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accptdata=accptdata+&#39;\n&#39;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for k in clients:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients[k].sendall(accptdata.encode(&#39;utf-8&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if obj not in outputs:&nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outputs.append(obj)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except ConnectionResetError:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; adr=obj.getpeername()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients={k:v for k,v in clients.items() if k!=adr }</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for b in clients:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clients[b].sendall(&#39;{}已退出&#39;.format(str(adr)).encode(&#39;utf-8&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;{}已退出&#39;.format(str(adr)))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;err&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not clients:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;最后一位{}已退出&#39;.format(str(adr)))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; inputs.remove(obj)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; &nbsp; &nbsp; with open(&#39;d:/chat/c.txt&#39;,&#39;r&#39;) as f:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accptall=f.read()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendmsg=accptall</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; &nbsp; &nbsp; c.sendall(sendmsg.encode(&#39;utf-8&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; c.close()</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">clients={}</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">outputs=[]</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">s=None</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">def main():</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global s</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global inputs</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; host=&#39;127.0.0.1&#39;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; port=8800</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; ##s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1024)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; s.bind((host,port))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; s.listen(5)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; inputs = [s]</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; tcpserve()</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">if __name__==&#39;__main__&#39;:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; main()</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <h3>
                                <span style="font-family: 宋体, SimSun;">客户端</span>
                            </h3>
                            <p>
                                <span style="font-size: 14px;">import socket</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">import threading</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##客户端</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">def sendmg():</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; while True:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sendmsg=input(&#39;输入发送内容：&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; s.sendall(bytes(sendmsg,encoding=&#39;utf-8&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;与服务器连接已断开&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">def receiv():</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; while True:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; acceptmsg=str(s.recv(1024),encoding=&#39;utf-8&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;\n&#39;.join(acceptmsg+&#39;\n&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(&#39;与服务器连接已断开&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">s=None</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">def main():</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; global s</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; host=&#39;127.0.0.1&#39;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">##&nbsp; &nbsp; port=8800</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; host=input(&#39;输入连接ip:&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; port=int(input(&#39;输入连接port:&#39;))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; print(&#39;端口请输入整数&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; return</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; try:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; s.connect((host,port))</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; except:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; print(&#39;连接失败，服务器没启动或者IP端口错误&#39;)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; &nbsp; &nbsp; return</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp;&nbsp;</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; ts=threading.Thread(target=sendmg)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; tr=threading.Thread(target=receiv)</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; ts.start()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; tr.start()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; ts.join()</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; tr.join()</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 14px;">if __name__==&#39;__main__&#39;:</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;">&nbsp; &nbsp; main()</span>
                            </p>
                            <p>
                                <br/>
                            </p>
                            <p>
                                <span style="font-size: 16px;">运行如图</span>
                            </p>
                            <p>
                                <span style="font-size: 14px;"><img src="../img/blogimg/201806131.jpg" style="width: 90%" title="" alt="201806131.jpg"/></span>
                            </p>
                            <p>
                                <br/>
                            </p>

                </div>
                <a href="javascript:void(0)" onclick="showPg()">返回</a>
            </div>


        </div>
    </div>
    <div class="footer">
        <div class="footertext">©2018,oixan博客</div>
    </div>
    <span id="backtop" onclick="topFunction()">返回顶部</span>
</body>
<script  src="../js/fanhuiye.js"></script>
</html>